**Present the elasticities results: 

*Set the directory to store the results: 
frame create results_elast
frame change results_elast
use "$root/Results/estimation_results/elasticities/elast_tot_abschange.dta", clear

frame create results_elast_w
frame change results_elast_w
use "$root/Results/estimation_results/elasticities/elast_w_abschange.dta", clear


**Add the two result frames together:
frame change results_elast
keep if own==1
gen wealth_group=0
frame change results_elast_w
keep if own==1

fframeappend _all, using(results_elast)


**Generate boxplots to illustrate the distribution of each wealth_group separate for each fuel type:

**Scale the elasticities to percent: 
replace elast=100*elast


**Create the graphs and tables. 

*Set the directory on where to save the graphs: 
cd "$root/Results/tables_graphs"
set scheme white_tableau

*For Gasoline cars:
forvalues i=0/4 {
	summarize elast if d_fuel==1 & wealth_group==`i'
	
	local mean`i'=round(`r(mean)',.01)
	
}

violinplot elast if d_fuel==1, over(wealth_group) noline nofill box mean(type(marker) msymbol(Dh) mcolor(black)) xtitle("Relative probability change (%)") olabels("Overall" "1st quartile" "2nd quartile" "3rd quartile" "4th quartile") text (0.2 `mean0' "Mean: `mean0'" -0.8 `mean1' "Mean: `mean1'" -1.8 `mean2' "Mean: `mean2'" -2.8 `mean3' "Mean: `mean3'" -3.8 `mean4' "Mean: `mean4'" , place(center) ) xscale(range(-3 -1.5)) xlabel(-3(.5)-1.5)

graph export "appendix/elast_abs_gasoline_avg.png", replace

*For Diesel cars:
forvalues i=0/4 {
	summarize elast if d_fuel==2 & wealth_group==`i'
	
	local mean`i'=round(`r(mean)',.01)
	
}

violinplot elast if d_fuel==2, over(wealth_group) noline nofill box mean(type(marker) msymbol(Dh) mcolor(black)) xtitle("Relative probability change (%)") olabels("Overall" "1st quartile" "2nd quartile" "3rd quartile" "4th quartile") text (0.2 `mean0' "Mean: `mean0'" -0.8 `mean1' "Mean: `mean1'" -1.8 `mean2' "Mean: `mean2'" -2.8 `mean3' "Mean: `mean3'" -3.8 `mean4' "Mean: `mean4'" , place(center))  xscale(range(-3 -1.5)) xlabel(-3(.5)-1.5)

graph export "appendix/elast_abs_diesel_avg.png", replace

*For EVs
forvalues i=0/4 {
	summarize elast if d_fuel==3 & wealth_group==`i'
	
	local mean`i'=round(`r(mean)',.01)
	
}

violinplot elast if d_fuel==3, over(wealth_group) noline nofill box mean(type(marker) msymbol(Dh) mcolor(black)) xtitle("Relative probability change (%)") olabels("Overall" "1st quartile" "2nd quartile" "3rd quartile" "4th quartile") text (0.2 `mean0' "Mean: `mean0'" -0.8 `mean1' "Mean: `mean1'" -1.8 `mean2' "Mean: `mean2'" -2.8 `mean3' "Mean: `mean3'" -3.8 `mean4' "Mean: `mean4'", place(center) ) xscale(range(-3 -1.5)) xlabel(-3(.5)-1.5)

graph export "appendix/elast_abs_ev_avg.png", replace


*For Hybrids
forvalues i=0/4 {
	summarize elast if d_fuel==4 & wealth_group==`i'
	
	local mean`i'=round(`r(mean)',.01)
	
}

violinplot elast if d_fuel==4, over(wealth_group)  noline nofill box mean(type(marker) msymbol(Dh) mcolor(black)) xtitle("Relative probability change (%)") olabels("Overall" "1st quartile" "2nd quartile" "3rd quartile" "4th quartile") text (0.2 `mean0' "Mean: `mean0'" -0.8 `mean1' "Mean: `mean1'" -1.8 `mean2' "Mean: `mean2'" -2.8 `mean3' "Mean: `mean3'" -3.8 `mean4' "Mean: `mean4'" , place(center) ) xscale(range(-3 -1.5)) xlabel(-3(.5)-1.5)

graph export "appendix/elast_abs_hybrid_avg.png", replace

**Do the same weighted: 

local type1 "gasoline"
local type2 "diesel"
local type3 "ev"
local type4 "hybrid"

forvalues drive=1/4 {
forvalues i=0/4 {
	summarize elast [fweight=d_car_reg] if d_fuel==`drive' & wealth_group==`i'
	
	local mean`i'=round(`r(mean)',.01)
	
}

violinplot elast [fweight=d_car_reg] if d_fuel==`drive', over(wealth_group) colors(Tab10, n(1) gscale) noline nofill box xtitle("Relative probability change (%)") olabels("Overall" "1st quartile" "2nd quartile" "3rd quartile" "4th quartile") text (0.2 `mean0' "Mean: `mean0'" -0.8 `mean1' "Mean: `mean1'" -1.8 `mean2' "Mean: `mean2'" -2.8 `mean3' "Mean: `mean3'" -3.8 `mean4' "Mean: `mean4'" , place(center) ) xscale(range(-3.0 -1.5)) xlabel(-3(.5)-1) 

graph export "main/elast_abs_`type`drive''_weight.png", replace

}


**Create the graph that illustrates elasticity as a function of price: 

*Gen variable to separate on: 

sepscatter elast d_price [fweight=d_car_reg] if wealth_group==0, separate(d_fuel) msymbol(oh dh th sh) msize(small small small small) legend(on pos(6) label(1 "Gasoline") label(2 "Diesel") label(3 "EV") label(4 "Hybrid") rows(1))  xtitle("Vehicle price (TCHF)") ytitle("Relative probability change (%)") 

graph export "appendix/elast_price.png", replace

sepscatter elast d_price if wealth_group>0, separate(wealth_group) msymbol(oh dh th sh) msize(small small small small) legend(on pos(6) label(1 "Quartile 1") label(2 "Quartile 2") label(3 "Quartile 3") label(4 "Quartile 4") rows(1))  xtitle("Vehicle price (TCHF)") ytitle("Relative probability change (%)") 

graph export "appendix/elast_price_bywealth.png", replace



**Also create a selection of overall own-cross price elasticity matrix: 
frame create elast_mat
frame change elast_mat
use elast_tot_abschange, clear

replace elast=100*elast

gcollapse (mean) elast (firstnm) d_car_reg d_fuel [fweight=s_car_reg], by(own d_option s_fuel)
gcollapse (mean) elast [fweight=d_car_reg], by(own d_fuel s_fuel)

frame put _all , into(cross)
keep if own==1
frame change cross
keep if own==0 
reshape wide elast, i(d_fuel) j(s_fuel)
drop own 
frame change elast_mat
frlink 1:1 d_fuel , frame(cross)
frget _all, from(cross)

*Now create the table: 

mkmat elast elast1 elast2 elast3 elast4, matrix(Tab_elast) 

matrix rownames Tab_elast =  gas diesel elec hy
foreach num of numlist 1(1)4 {
foreach numb of numlist 1(1)5 {
matrix Tab_elast[`num',`numb']=round(Tab_elast[`num',`numb'],.0001)
}
}

estadd matrix Tab_elast, replace
#delim ;
estout matrix(Tab_elast, fmt(%09.0gc)) using "appendix/elast_abs_avg.tex", 
style(tex)  mlabels(,none) cells(e(Tab_semi)(fmt(%09.5f)))
varlabels ( gas "Gasoline"
			diesel "Diesel"
			elec "Electric"
			hy "Hybrid"
             )
collabels ( "Own" 
			"Cross Gasoline" 
			 "Cross Diesel"  
			 "Cross Electric" 
			 "Cross Hybrid")
prehead("\begin{tabular}{@{}lccccc@{}} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular}") replace;
#delim cr


*Also create a selection of matrix of own- / cross-price elasticities: 

frame create elast_sect
frame change elast_sect
use elast_tot_abschange, clear

replace elast=100*elast


*Only keep the observations of each top2 model within fuel_type:
keep if d_option==471 | d_option==477 | d_option==160 | d_option==395 | d_option==374 | d_option==428 | d_option==446 | d_option==443

keep if s_option==471 | s_option==477 | s_option==160 | s_option==395 | s_option==374 | s_option==428 | s_option==446 | s_option==443


keep elast d_option s_option d_fuel

reshape wide elast, i(d_option) j(s_option)

sort d_fuel d_option


mkmat elast471 elast477 elast160 elast395 elast374 elast428  elast443 elast446, matrix(Tab_select) 

matrix rownames Tab_select =  gas1 gas2 diesel1 diesel2 elec1 elec2 hy1 hy2
foreach num of numlist 1(1)8 {
foreach numb of numlist 1(1)8 {
	matrix Tab_select[`num',`numb']=round(Tab_select[`num',`numb'],.0001)

}
}

estadd matrix Tab_select, replace
#delim ;
estout matrix(Tab_select, fmt(%09.0gc)) using "appendix/elast_abs_selection.tex", 
style(tex)  mlabels(,none) cells(e(Tab_select)(fmt(%09.5f)))
varlabels ( gas1 "VW Golf"
			gas2  "VW Polo"
			diesel1 "Ford Kuga"
			diesel2 "Skoda Octavia"
			elec1 "Renault Zoe"
			elec2 "Tesla Model 3"
			hy1 "Toyota Rav-4"
			hy2 "Toyota Yaris"
             )
collabels ( "VW Golf" 
			"VW Polo" 
			 "Ford Kuga"  
			 "Skoda Octavia" 
			 "Renaule Zoe"
			 "Tesla Model 3"
			 "Toyota Rav-4"
			 "Toyota Yaris"
			 )
prehead("\begin{tabular}{@{}lccccc@{}} \toprule")  
posthead("\midrule")
prefoot("\bottomrule") 
postfoot("\end{tabular}") replace;
#delim cr


